In [39]:
import os
import pandas as pd
from pandas.tools.plotting import scatter_matrix
import matplotlib.pyplot as plt
In [40]:
path = "C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script"
print(os.getcwd(),path)

#This function get the data in excel file
def get_dataset( data_id, path):
        # if data_is = 1: fetch location.csv
        # if data_is = 2: fetch classes.csv
        #C:\Users\leadlab\Documents\GitHub\bcopph-mhsu-surveillance\data-public\data-use-4-python-script
        filepath_location = path +"/annual-location-count-2018-03-27-locations.csv"
        filepath_classes = path +"/annual-location-count-2018-03-27-classes.csv"
        filepath_map = path+"/compressor_map.csv"
        
        if(data_id == 1):
            dataset = pd.read_csv(filepath_classes, header = 0, sep = ',',error_bad_lines=False)
        elif(data_id == 2):
            dataset = pd.read_csv(filepath_location, header = 0, sep = ',',error_bad_lines=False)
        elif(data_id ==3):
            dataset = pd.read_csv(filepath_map, header = 0, sep = ',',error_bad_lines=False)

        #print(dataset.head(30))
        dataset = dataset.dropna()
        col1 = dataset['location_class_description']
        #print(col1.unique().tolist())
        #print (col1.shape)
        col1_list = col1.unique().tolist()
        return dataset
C:\Users\leadlab C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script
In [41]:
fecth_data = get_dataset(3,path)
print(fecth_data.head())
print (fecth_data.shape)
   location_map_id  location_class_code  \
0                1                   26   
1                2                   78   
2                3                  139   
3                4                  148   
4                5                   25   

                          location_class_description          intensity_type  \
0       Residential Care - MHSU - Rental Supplements        Residential Care   
1                                      ED - Med-Surg  ED, Urgent Care, Acute   
2                   Surgery - Procedure - Mixed Ages                 Surgery   
3                                    Medical Imaging         Medical Imaging   
4  Addictions - Post Withdrawal Stabilization (se...        Residential Care   

             intensity_severity_risk      clinical_focus  \
0                  Rental Supplement                MHSU   
1                  Emergent-Hospital  Emergency Response   
2  Surgery-Procedure-Acute Admission            Surgical   
3                    Medical Imaging     Medical Imaging   
4          Medium Intensity Res Care     MHSU-Addictions   

               service_type    service_location  \
0    Rental Supplement-MHSU           Community   
1                ED-Medical            Hospital   
2         Surgery-Procedure            Hospital   
3           Medical Imaging     Medical Imaging   
4  Res Care-MHSU-Post Detox  Community Facility   

                     population_age  \
0  Adults, some adols, older adults   
1                        Mixed Ages   
2                        Mixed Ages   
3                        Mixed Ages   
4  Adults, some adols, older adults   

                                        provider_mix  n_unique  
0  Nursing and allied health professional, physic...         1  
1                         Physician, nurse delivered         1  
2                         Physician, nurse delivered         1  
3              Medical specialist with tech supports         1  
4  Nursing and allied health professional, physic...         1  
(2231, 11)
In [42]:
#This function is cleans the file name before printing
def clean_file_b4_used(filename):
    #print (list(filename))
    special_character = ['&','/','(',')','*','%','@','^','#','!'] # This is the list of special character that supposed not to be included in a file name
    new_fname = ""
    for i in list(filename):
        if any(i in s for s in special_character):
            a=0
        else:
            new_fname = new_fname + i
    return new_fname

#This function is to get the list of all the class code in the dataframe
def get_class_code_list(dataset):
    location_class_code_unique = dataset['location_class_code'].unique().tolist() # This get the list of unque class code or class id
    return location_class_code_unique

def visualization(dataset, class_code_id,filepath):
    
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111)
    dataset["event_year"] = (dataset['event_year'].astype('str'))
    #print (dataset.head(10))
    
    df= (dataset.where(( dataset["location_class_code"] == int(class_code_id)))).copy()
    location_class_description = df['location_class_description'].unique().tolist()
    class_name = clean_file_b4_used((str(location_class_description[1])))
    
    title = "Trend Analytics For " + class_name + "-"+str(class_code_id)
    #print ("Unique Para Type",type(location_class_description), location_class_description, len(df.index), (location_class_code_unique))
    plt.xlabel("Time")
    plt.ylabel("Parameter Total Number")
    #plt.legend(loc='best')
    #ax.legend(loc='best')
    plt.gca().legend(loc='upper left')
    ax.set_title(title)
    plt.xticks(rotation=90)
    df =df.dropna()
    # red dashes, blue squares and green triangles
    plt.plot(df["event_year"], df["n_people"], 'r', df["event_year"], df["n_encounters"], 'ms', df["event_year"], df["n_locations"], 'k^')
    plt.show()
    #for row in dataset.itertuples(): 
    #print(df.head(10))
    #print("Data Seize",df.shape)
    
    
    fig = plt.figure(figsize=(10,5))
    ax = fig.add_subplot(111)
    plt.subplot(3, 1, 1)
    plt.plot(df["event_year"], df["n_people"], 'r')
    plt.title(title)
    plt.ylabel('n_people')
    #plt.xticks(rotation=90)
    
    plt.subplot(3, 1, 2)
    plt.plot(df["event_year"], df["n_encounters"], 'b-')
    plt.xlabel('time (s)')
    plt.ylabel('n_encounter')
    #plt.xticks(rotation=90)
    
    plt.subplot(3, 1, 3)
    plt.plot(df["event_year"], df["n_locations"], 'k')
    plt.xlabel('time (s)')
    plt.ylabel('n_location')
    plt.xticks(rotation=90)
    plt.savefig(filepath+"/"+class_name)
    plt.show()

#Function to display the images
def display(filepath,path):
    print("*********************************************************************************")
    data_classes = get_dataset(1,path) #classes
    data_location= get_dataset(2,path) #location
    data_map = get_dataset(3,path) #maping
    #class_code = 66
    #visualization(data_location,class_code)
    print("********************************************************************************")
    class_code_list = get_class_code_list(data_classes)
    for i in class_code_list:
        visualization(data_classes,i,filepath)
    
    
In [43]:
filepath_2save_images = path + "/class_report_printout" # This is the path to save the image files generated
print (filepath_2save_images)
display(filepath_2save_images,path)
C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script/class_report_printout
*********************************************************************************
********************************************************************************
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
No handles with labels found to put in legend.
---------------------------------------------------------------------------
OSError                                   Traceback (most recent call last)
<ipython-input-43-22b968380805> in <module>()
      1 filepath_2save_images = path + "/class_report_printout" # This is the path to save the image files generated
      2 print (filepath_2save_images)
----> 3 display(filepath_2save_images,path)

<ipython-input-42-194b6d10149d> in display(filepath, path)
     78     class_code_list = get_class_code_list(data_classes)
     79     for i in class_code_list:
---> 80         visualization(data_classes,i,filepath)
     81 
     82 

<ipython-input-42-194b6d10149d> in visualization(dataset, class_code_id, filepath)
     64     plt.ylabel('n_location')
     65     plt.xticks(rotation=90)
---> 66     plt.savefig(filepath+"/"+class_name)
     67     plt.show()
     68 

~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in savefig(*args, **kwargs)
    699 def savefig(*args, **kwargs):
    700     fig = gcf()
--> 701     res = fig.savefig(*args, **kwargs)
    702     fig.canvas.draw_idle()   # need this if 'transparent=True' to reset colors
    703     return res

~\Anaconda3\lib\site-packages\matplotlib\figure.py in savefig(self, fname, **kwargs)
   1832             self.set_frameon(frameon)
   1833 
-> 1834         self.canvas.print_figure(fname, **kwargs)
   1835 
   1836         if frameon:

~\Anaconda3\lib\site-packages\matplotlib\backend_bases.py in print_figure(self, filename, dpi, facecolor, edgecolor, orientation, format, **kwargs)
   2265                 orientation=orientation,
   2266                 bbox_inches_restore=_bbox_inches_restore,
-> 2267                 **kwargs)
   2268         finally:
   2269             if bbox_inches and restore_bbox:

~\Anaconda3\lib\site-packages\matplotlib\backends\backend_agg.py in print_png(self, filename_or_obj, *args, **kwargs)
    510         renderer.dpi = self.figure.dpi
    511         if isinstance(filename_or_obj, six.string_types):
--> 512             filename_or_obj = open(filename_or_obj, 'wb')
    513             close = True
    514         else:

OSError: [Errno 22] Invalid argument: 'C:/Users/leadlab/Documents/GitHub/bcopph-mhsu-surveillance/data-public/data-use-4-python-script/class_report_printout/MHSU Rehab Services - High Intensity tertiary level???.png'